package com.retailshop.shopuser.WapUserController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopuser.common.MessageConstants;
import com.retailshop.shopuser.common.Response;
import com.retailshop.shopuser.common.StringUtil;
import com.retailshop.shopuser.common.UserLoginToken;
import com.retailshop.shopuser.entity.SysMessage;
import com.retailshop.shopuser.entity.UserSysMessage;
import com.retailshop.shopuser.service.SysMessageService;
import com.retailshop.shopuser.service.UserSysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/wap")
public class MessageController {

    @Autowired
    SysMessageService sysMessageService;

    @Autowired
    UserSysMessageService userSysMessageService;


    /**
     * 查询用户未读消息
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getSysMessage")
    public Response getSysMessage(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String pages = request.getParameter("page");
        String limits = request.getParameter("limit");
        Map<String, Object> map = new HashMap<>();
        int userNoId = 0;
        int page = 1;
        int limit = 5;
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds) ||
                pages == null || pages == "" || !StringUtil.isNumeric(pages) ||
                limits == null || limits == "" || !StringUtil.isNumeric(limits)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        userNoId = Integer.parseInt(userNoIds);
        page = Integer.parseInt(pages);
        limit = Integer.parseInt(limits);
        List<SysMessage> sysMessageList = null;
        //查询未读消息
        PageHelper.startPage(page, limit);
        sysMessageList = sysMessageService.getSysMessage(userNoId);
        System.out.println("————-->" + sysMessageList.isEmpty());

        if (sysMessageList.isEmpty() && page == 1) {
            //默认查询两条
            sysMessageList = sysMessageService.getDefaultSysMessage(userNoId);

        }
        PageInfo<SysMessage> pageInfo = new PageInfo<>(sysMessageList);


        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }

    /**
     * 更新消息状态
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/updateSysMessage")
    public Response updateSysMessage(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String sysMessageIds = request.getParameter("sysMessageId");
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds) || sysMessageIds == null || sysMessageIds == ""
                || !StringUtil.isNumeric(sysMessageIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        int sysMessageId = 0;

        userNoId = Integer.parseInt(userNoIds);
        sysMessageId = Integer.parseInt(sysMessageIds);

        UserSysMessage userSysMessage = new UserSysMessage();
        userSysMessage.setUserNoId(userNoId);
        userSysMessage.setSysMessageId(sysMessageId);
        userSysMessage.setUserSysState("1");
        int row = userSysMessageService.updateMessage(userSysMessage);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }

    /**
     * 读取消息
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getSysMessageList")
    public Response getSysMessageList(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String pages = request.getParameter("page");
        String limits = request.getParameter("limit");
        int userNoId = 0;
        int page = 1;
        int limit = 5;
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds) ||
                pages == null || pages == "" || !StringUtil.isNumeric(pages) ||
                limits == null || limits == "" || !StringUtil.isNumeric(limits)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        userNoId = Integer.parseInt(userNoIds);
        page = Integer.parseInt(pages);
        limit = Integer.parseInt(limits);
        List<SysMessage> sysMessageList = null;
        //查询未读消息
        PageHelper.startPage(page, limit);
        sysMessageList = sysMessageService.geSysMessageList(userNoId);

        if (sysMessageList.isEmpty() && page == 1) {
            //默认查询两条
            sysMessageList = sysMessageService.getDefaultSysMessage(userNoId);

        }

        PageInfo<SysMessage> pageInfo = new PageInfo<>(sysMessageList);


        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }

    @ResponseBody
    @RequestMapping("/getSysMessageCount")
    public Response getSysMessageCount(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        userNoId = Integer.parseInt(userNoIds);
        int sum = userSysMessageService.getSysMessageCount(userNoId);

        return new Response(MessageConstants.MSG_SUCCESS, sum, 0, "", request);
    }


    @ResponseBody
    @RequestMapping("/addSysMessage")
    public Response addSysMessage(HttpServletRequest request) {

                String content = "系统公告：亲！在商城中遇到问题请联系我们的客服";
        String img = "http://resources.ycsqd.com/201905281013365680.jpg";
        SysMessage sysMessage = new SysMessage();
        sysMessage.setCreatTime(new Timestamp(System.currentTimeMillis()));
        sysMessage.setMessageCategory("1");
        sysMessage.setMessageContent(content);
        sysMessage.setMessageImg(img);
        sysMessage.setMessageState("0");
        sysMessage.setMessageUrl("www.ycsqd.com");
        sysMessage.setTitle("系统公告");

        return null;
    }

}
